#include "commonpage.h"
#include "ui_commonpage.h"
#include "listitembox.h"

#include <QDebug>

CommonPage::CommonPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CommonPage)
{
    ui->setupUi(this);

    //播放所有
    connect(ui->playAllBtn, &QPushButton::clicked, this, [=]()
    {
        emit playAllMusic(pageType);
    });

    //双击播放
    connect(ui->pageMusicList, &QListWidget::doubleClicked, this, [=](const QModelIndex& index)
    {
        emit playMusicByIndex(this, index.row());
    });
}

CommonPage::~CommonPage()
{
    delete ui;
}

void CommonPage::setCommonPageUI(const QString &title, const QString &imagePath)
{
    ui->pageTitle->setText(title);
    ui->musicPictureLabel->setPixmap(QPixmap(imagePath));

    ui->musicPictureLabel->setScaledContents(true);
}

//更新界面歌曲
void CommonPage::upDateMusic(MusicList &musicList)
{
    ui->pageMusicList->clear();
    addMusicToMusicPage(musicList);

    for(auto musicId : musicListOfPage)
    {
        auto it = musicList.findMusicByMusicId(musicId);

        if(it == musicList.end())
            continue;

        //将歌曲信息显示在 pageMusicList 中
        ListItemBox* listItemBox = new ListItemBox(this);

        listItemBox->setMusicName(it->getMusicName());
        listItemBox->setMusicSinger(it->getMusicSinger());
        listItemBox->setMusicAlbum(it->getMusicAlbum());
        listItemBox->setWhetherLike(it->getIsLike());

        QListWidgetItem* item = new QListWidgetItem(ui->pageMusicList);
        item->setSizeHint(QSize(listItemBox->width(), listItemBox->height()));
        ui->pageMusicList->setItemWidget(item, listItemBox);

        // likeOrDisLike 发生改变，发送信号 upDateLikeMusic 给 mrMusic 让其处理
        connect(listItemBox, &ListItemBox::likeOrDisLike, this, [=](bool isLike)
        {
            emit upDateLikeMusic(isLike, it->getMusicId());
        });
    }
    //窗口重绘
    repaint();
}

void CommonPage::setPageType(PageType pageType)
{
    this->pageType = pageType;
}

void CommonPage::addMusicToMusicPage(MusicList &musicList)
{
    musicListOfPage.clear();
    for(auto& music : musicList)
    {
        switch(pageType)
        {
            case LIKE_PAGE:
                if(music.getIsLike())
                {
                    musicListOfPage.push_back(music.getMusicId());
                }
                break;
            case LOCAL_PAGE:
                    musicListOfPage.push_back(music.getMusicId());
                break;
            case RECENT_PAGE:
                if(music.getIsHistory())
                {
                    musicListOfPage.push_back(music.getMusicId());
                }
                break;
            default: qDebug() << "暂未支持";
         }
    }
}

void CommonPage::addMusicToPlaylist(MusicList &musicList, QMediaPlaylist *playlist)
{
    for(auto& music : musicList)
    {
        switch(pageType)
        {
            case LIKE_PAGE:
                if(music.getIsLike())
                {
                    playlist->addMedia(music.getMusicUrl());
                }
                break;
            case LOCAL_PAGE:
                    playlist->addMedia(music.getMusicUrl());
                break;
            case RECENT_PAGE:
                if(music.getIsHistory())
                {
                    playlist->addMedia(music.getMusicUrl());
                }
                break;
            default: qDebug() << "暂未支持";
         }
    }
}

QString CommonPage::getMusicByIndex(int index)
{
    if(index >= musicListOfPage.size())
    {
        qDebug() << "无此歌曲";
        return "";
    }
    return musicListOfPage[index];
}

void CommonPage::setMusicImage(QPixmap pixmap)
{
    ui->musicPictureLabel->setPixmap(pixmap);
    ui->musicPictureLabel->setScaledContents(true);
}
